﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;


/// <summary>
/// messages类纪录的是信息表的相关信息和操作
/// </summary>
public class messages
{
    
    private string _TRUEFLOWID;
    private string _FROMUSERNAME;
    private string _TOUSERNAME;
    private string _TITLE;
    private string _CONTENT;
    private DateTime _SENDDATE;
    private int _FROMUSERSTATUS;
    private int _TOUSERSTATUS;
   
    //获取当年的表名
    private    string strTableName = "MESSAGE" + Convert.ToString(DateTime.Now.Year);
   

    #region
    public string TrueFlowID
    {
        set { _TRUEFLOWID = value; }
        get { return _TRUEFLOWID; }
    }
    public string FromUserName
    {
        set { _FROMUSERNAME = value; }
        get { return _FROMUSERNAME; }
    }
    public string ToUserName
    {
        set { _TOUSERNAME = value; }
        get { return _TOUSERNAME; }
    }
    public string Title
    {
        set { _TITLE = value; }
        get { return _TITLE; }
    }
    public string Content
    {
        set { _CONTENT = value; }
        get { return _CONTENT; }
    }
    public DateTime SendDate
    {
        set { _SENDDATE = value; }
        get { return _SENDDATE; }
    }
    public int FromUserStatus
    {
        set { _FROMUSERSTATUS = value; }
        get { return _FROMUSERSTATUS; }
    }
    public int ToUserStatus
    {
        set { _TOUSERSTATUS = value; }
        get { return _TOUSERSTATUS; }
    }
 
    #endregion
    public messages()
    {

       //
        // TODO: 在此处添加构造函数逻辑
        //
    }
    public messages(string trueFloeID, string fromUserName, string toUserName, string title, string content, DateTime sendDate, int fromUserStatus, int toUserStatus)
    {
        _TRUEFLOWID = trueFloeID;
        _FROMUSERNAME = fromUserName;
        _TOUSERNAME = toUserName;
        _TITLE = title;
        _CONTENT = content;
        _SENDDATE = sendDate;
        _FROMUSERSTATUS = fromUserStatus;
        _TOUSERSTATUS = toUserStatus;
       

    }
    /// <summary>
    /// 查找当前用户所有未查看的信息
    /// </summary>
    /// <param name="userName">当前用户名</param>
    /// <returns>返回一个记录当前用户所有未查看信息的dataset</returns>
    public DataSet SearchNoReadMsg()
    {

        string strSql = "select top 10 * from " + strTableName + " where TOUSERNAME='" + HttpContext.Current.Session["userName"].ToString() + "'  and TOUSERSTATUS=0 order by SENDDATE desc";
        DBOperation db = new DBOperation();
        string tableName = "NoReadMessages";
        return db.GetDataSet(strSql,tableName);
    }
    /// <summary>
    /// 查找当前用户已经查看过的信息
    /// </summary>
    /// <returns>返回一个记录当前用户所有已查看信息的dataset</returns>
    public DataSet SearchReadedMsg()
    {
       

        string strSql = "select * from " + strTableName + " where TOUSERNAME='" + HttpContext.Current.Session["userName"].ToString() + "' and TOUSERSTATUS=1";
        DBOperation db = new DBOperation();
        string tableName = "readedMessage";
        return db.GetDataSet(strSql,tableName);
    }
    /// <summary>
    /// 添加一条信息(相当于发送一条信息)
    /// </summary>
    /// <returns>添加成功返回真，否则假</returns>
    public bool AddMsg()
    {
        bool seccess = false;
        string strSql = "insert into " + strTableName + "(TRUEFLOWID,FROMUSERNAME,TOUSERNAME,TITLE,CONTENT,SENDDATE,FROMUSERSTATUS,TOUSERSTATUS)values("
                         + "'" + this.TrueFlowID + "',"
                         + "'" + this.FromUserName + "',"
                         + "'" + this.ToUserName + "',"
                         + "'" + this.Title + "',"
                         + "'" + this.Content + "',"
                         + "'" + this.SendDate + "',"
                         + "'" + this.FromUserStatus + "',"
                         + "'" + this.ToUserStatus + "')";
                       

        DBOperation db = new DBOperation();
        if (db.ExcuteSql(strSql))
        {
            seccess = true;

        }
        return seccess;
    }
    /// <summary>
    /// 修改信息
   /// <returns>修改成功返回真，否则假</returns>
    public bool AlterMsg()
    {
        bool seccess = false;
        string strSql = "update " + strTableName + " set"
                      + " TRUEFLOWID='" + this.TrueFlowID + "',"
                      + "FROMUSERNAME='" + this.FromUserName + "',"
                      + "TOUSERNAME='" + this.ToUserName + "',"
                      + "TITLE='" + this.Title + "',"
                      + "CONTENT='" + this.Content + "',"
                      + "SENDDATE='" + this.SendDate + "',"
                      + "FROMUSERSTATUS='" + this.FromUserStatus + "',"
                      + "TOUSERSTATUS='" + this.ToUserStatus + "'";
      
        DBOperation db = new DBOperation();

        if (db.ExcuteSql(strSql))
        {
            seccess = true;
        }
        return seccess;
    }
   /// <summary>
   ///根据信息id 删除信息
   /// </summary>
   /// <param name="msgID">需要删除的信息的ID</param>
   /// <returns>删除成功返回真，否则假</returns>
    public bool DeleteMsg(int msgID)
    {
        bool seccess = false;
        string strSql = "delete from " + strTableName + " where AUTOID=" + msgID;

        DBOperation db = new DBOperation();
        if (db.ExcuteSql(strSql))
        {
            seccess = true;
        }
        return seccess;
    }
    /// <summary>
    /// 统计当前用户未读信息数量
    /// </summary>
    /// <returns>返回未读信息数量</returns>
    public int  CountNoReadMsg()
    {
        string strCount = "select count(*) from " + strTableName + " where TOUSERNAME='" + HttpContext.Current.Session["userName"].ToString() + "' and TOUSERSTATUS=0";
        DBOperation db=new DBOperation();
        return  Convert.ToInt32( db.ExcuteScalar(strCount));
    }
    /// <summary>
    /// 统计用户已经阅读过的信息的数量
    /// </summary>
    /// <param name="userName">当前用户名</param>
    /// <returns></returns>
    public int CountReadedMsg()
    {
        string strCount = "select count(*) from " + strTableName + " where TOUSERNAME='" + HttpContext.Current.Session["userName"].ToString() + "' and TOUSERSTATUS=1";
        DBOperation db = new DBOperation();
        return Convert.ToInt32(db.ExcuteScalar(strCount));
    }
    /// <summary>
    /// 自动发送跟审核有关的消息，参数：待审核文件ID，发送者，接收者，标题，内容
    /// </summary>
    /// <returns>成功添加则返回true，否则返回false</returns>
    public Boolean SendCheckDataMessage()
    {
        Boolean success = true;
        string strSql = "insert into " + strTableName + "(TRUEFLOWID,FROMUSERNAME,TOUSERNAME,TITLE,CONTENT,FROMUSERSTATUS,TOUSERSTATUS) values ("
                         + "'" + this.TrueFlowID + "',"
                         + "'" + this.FromUserName + "',"
                         + "'" + this.ToUserName + "',"
                         + "'" + this.Title + "',"
                         + "'" + this.Content + "',"                         
                         + "1,0)";
        DBOperation DBO = new DBOperation();
        if (DBO.ExcuteSql(strSql))
        {
            success = true;
        }
        else
        {
            success = false;
        }
        return success;
    }


   


}

